/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]
	
Output: Table 1, C.1, C.2, C.3, C.4

*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
*	 Prep Estimation *
********************************************************************************
			
	// compute id for DISTANCE increase/decrease, 0 else
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"	
	
	// compute id for DISTANCE: 3 groups
	cap drop tmp*
	_pctile del_street_dist							if K==0 & del_street_dist<.7, n(3)
	gen 	tmp1 = (del_street_dist>r(r2)) 			if K==0
	bys sb_new (tmp1): gen ind_dist_m3 = tmp1[1]
	replace ind_dist_m3 = 0 						if missing(Ei)
	
	gen 	tmp2 = (inrange(del_street_dist,r(r1),r(r2))) if K==0
	bys sb_new (tmp2): gen ind_dist_m2 = tmp2[1]						
	replace ind_dist_m2 = 0 						if missing(Ei)
	
	gen 	tmp3 = (del_street_dist<r(r1)) 			if K==0
	bys sb_new (tmp3): gen ind_dist_m1 = tmp3[1]
	replace ind_dist_m1 = 0 						if missing(Ei)		
	

********************************************************************************
*	 Baseline: Event Study Table (Table C1)
********************************************************************************
*	
	global order F4event F3event F2event /*F1event*/ L0event L1event L2event	
	
	cap program drop extract_N
	program extract_N, rclass
	/*
		Little program to extract and returns #precincts in control and treatm group
		based on e(sample)
		set 50 or 100 depending on whether event = 100% or 50+% reassgn.
	
	*/
		syntax anything(name=treat)
		
		assert inlist(`treat',50,100)
			
		if `treat'==100 {
			 distinct(sb_new) if e(sample) & K==0	// count TREAT precincts
			local T = r(ndistinct)
			 distinct(sb_new) if e(sample) & K==.	// count CTRL precincts
			local C = r(ndistinct)
		}
		else {
			 distinct(sb_new) if e(sample) & K50==0	// count TREAT precincts
			local T = r(ndistinct)
			 distinct(sb_new) if e(sample) & K50==.	// count CTRL precincts
			local C = r(ndistinct)		
		}
		
		return local N_T "`T'"
		return local N_C "`C'"
	end
		
	
	* TABLE C1. Baseline Event Study Results
foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	outreg, clear
	
	*>>>> gen leads and lags: treat=100%
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen 	F`l'event = K==-`l'
		lab var F`l'event "Reassignment (#t-`l'#)"
	}	
	forvalues l = 0/7 {
		gen 	L`l'event = K==`l'
		lab var L`l'event "Reassignment (#t+`l'#)"
	}		
	drop 	F1event // drop reference period
	
		// (1) Event study -- ELECTION-DISTRICT FE + NO Weights 
		 reghdfe `v' F* L* 					$ctr if smpl_trim==1 	, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, )	
		
		// (2) Event study -- ELECTION-DISTRICT FE + WITH controls [MAIN EQ 1]
		 reghdfe `v' F* L* 					$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100		
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, X)	 		

		// (3) Event study -- ELECTION-DISTRICT FE + FULL event window (not dropped)
		 reghdfe `v' F* L* 					$ctr  /*if smpl_trim==1*/ 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, X \ Full sample, X)	 		
		
		// (4) Event study -- ONLY ELECTION FE + WITH controls [MAIN EQ 2]
		 reghdfe `v' F* L* 					$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election FE, X \ Weights, X)	 
		
		// (5) Event study -- Eq (2) but remove CTRL precincts with some NONZERO reass and TREAT precinct with 1+ treatment
		 reghdfe `v' F* L* 					$ctr if cleanctr==1 & fulltottreat100<=1 $wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, X \ Clean sample, X)
		
		// (6) Event study -- Balanced Sample (not trimmed, TREAT precincts with exactly 1 treatment)
		 reghdfe `v' F* L* 					$ctr if smpl_bal==1 & fulltottreat100<=1 $wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, X \ Balanced sample, X)		
		
		// (7) Event study -- Eq (2) but remove TREAT precincts w/ boundary change in t=0
		 reghdfe `v' F* L* 					$ctr if sb_const_size==1 & smpl_trim==1 $wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		extract_N 100
		 outreg,  $opt  keep($order) 	addrow(#treated precincts, `r(N_T)' \ #control precincts, `r(N_C)' \ Precinct FE, X \ Election-District FE, X \ Weights, X \ No boundary change, X)			
	

  * save
	 outreg, replay replace store(`v')
}
		 outreg, replay(turnout_urne) ctitle("", (1), (2), (3), (4), (5), (6), (7) \ "\midrule" \ "\multicolumn{3}{l}{\textbf{Panel A:} Effect on Turnout at the Polling Place}" \"\midrule") store(tab1) ///
				addrow("\midrule" \ "\multicolumn{3}{l}{\textbf{Panel B:} Effect on Turnout via Mail}" \ "\midrule") 
		 outreg, replay(tab1) append(turnout_pos_req) store(tab_urne_postal_req)
		
		 outreg, replay(tab_urne_postal_req) store(tab3) ///
				addrow("\midrule" \ "\multicolumn{3}{l}{\textbf{Panel C:} Effect on Total Turnout}" \ "\midrule")  
		 outreg, replay(tab3) append(turnout_tot_req) store(tab_urne_postal_tot_req)
	
	
	// Display+Export Table
	outreg using "$tables/Table_C1_ES_bsl", replay(tab_urne_postal_tot_req) replace tex fragment      
	// clean .tex table for Overleaf 
	cleantex "$tables/Table_C1_ES_bsl.tex" , nodisplay replace
	


********************************************************************************
*	 Event Study Table CONDITIONAL on log street distance (Table 1)
********************************************************************************
	
	* TABLE 1. Event Study Estimates Conditional on Log Walking Distance

	global order  F4event F3event F2event /*F1event*/ L0event L1event L2event 
	
outreg, clear			
foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
			
	*gen leads and lags: 100%
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen 	F`l'event = K==-`l'
		lab var F`l'event "Reassignment (#t-`l'#)"
	}	
	forvalues l = 0/7 {
		gen 	L`l'event = K==`l'
		lab var L`l'event "Reassignment (#t+`l'#)"
	}		
	drop 	F1event // drop reference period
	
	// replicate baseline (no dist control) for comparision [not in output, just for computing fraction explained]
	 reghdfe `v' F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		estimates store `v'b_fe_de

	 reghdfe `v'  F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id i.sb_new) cluster(sb_new) 
		estimates store `v'b_fe_e

		
		// (1) Event study  -- ELECTION-DISTRICT FE + Controlling for distance
		 reghdfe `v' ln_street_dist F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		 outreg,  $opt  keep(ln_street_dist $order) 	addrow(Precinct FE, X \ Election-District FE, X \ Controls, X)	
		estimates store `v'_fe_de
			
	
		// (2) Event study  -- ONLY ELECTION FE + Controlling for distance
		 reghdfe `v' ln_street_dist F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id i.sb_new) cluster(sb_new) 
		 outreg,  $opt  keep(ln_street_dist $order) 	addrow(Precinct FE, X \ Election FE, X \ Controls, X)
		estimates store `v'_fe_e
		
}		
outreg, replay  ctitle("", "\multicolumn{2}{c}{\makecell{Turnout \\ at the Polling Place}}","","\multicolumn{2}{c}{\makecell{Turnout \\ by Mail}}","", "\multicolumn{2}{c}{\makecell{Total\\ Turnout}}" \ ///
						"\cline{2-3} \cline{4-5} \cline{6-7}" \ "", (1), (2), (3), (4), (5), (6)) store(tab) note("@{\extracolsep{4pt}}lcccccc@{}")		
outreg using "$tables/Table_1_ES_ctr_ln_street_dist", replay(tab) tex replace	fragment


	// auxillary tests of L1event-L0event for mail-in and in-person after absorbing log_street dist
	 estimates restore turnout_pos_req_fe_de
	 lincom L1event-L0event
	di stritrim("Mail-in: tau1-tau0:`:di %12.2f `r(estimate)'', p=`:di %12.3f `r(p)''") // Mail-in: tau1-tau0: 0.41, p= 0.012
	 estimates restore turnout_urne_fe_de
	 lincom L1event-L0event
	di stritrim("In-person: tau1-tau0:`:di %12.2f `r(estimate)'', p=`:di %12.3f `r(p)''") // In-person: tau1-tau0: -0.04, p= 0.791
	
	
 ** compute fraction of effect explained by distance
	cap frame drop ctr_eq
	cap frame drop bsl_eq
	frame create bsl_eq str25 outcome str10 model str15 estimate coef_bsl 	
	frame create ctr_eq str25 outcome str10 model str15 estimate coef_ctr 	
	
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		estimates restore `v'b_fe_de // bsl elec-district FE
		forvalues j = 0/2 {
			frame post bsl_eq ("`v'") ("D-E FE") ("L`j'event") (_b[L`j'event]) 
		}
		estimates restore `v'b_fe_e
		forvalues j = 0/2 {
			frame post bsl_eq ("`v'") ("E FE") ("L`j'event") (_b[L`j'event]) 
		}		
		estimates restore `v'_fe_de
		forvalues j = 0/2 {
			frame post ctr_eq ("`v'") ("D-E FE") ("L`j'event")  (_b[L`j'event]) 
		}		
		estimates restore `v'_fe_e
		forvalues j = 0/2 {
			frame post ctr_eq ("`v'") ("E FE") ("L`j'event")  (_b[L`j'event]) 
		}	
	}
	frame bsl_eq{
		frlink 1:1 outcome model estimate, frame(ctr_eq)
		frget coef_ctr, from(ctr_eq)
		
		gen frac_dist = 1-(coef_ctr/coef_bsl)
		drop if outcome=="turnout_tot_req" & estimate!="L0event"	// keep only L0event for total
		bys outcome model: egen tot_frac_dist=mean(frac_dist)		// AVG frac explained by dist 
		sort model outcome  estimate
		list
		
		levelsof outcome, local(outc)
		foreach v of local outc {
			preserve
				keep if outcome=="`v'"
				duplicates drop model, force
				sort model
				local `v'_fe_de =`:di %9.2g tot_frac_dist[1]'
				local `v'_fe_e =`:di %9.2g tot_frac_dist[2]'
			restore
		}
	}
	
	// Export Table: replay table from above and add fraction explained by distance	
	outreg using "$tables/Table_1_ES_ctr_ln_street_dist", replay(tab) tex replace	fragment ///
		addrow("\makecell[l]{Fraction of effect\\explained by distance}", ///
				`turnout_urne_fe_de' , `turnout_urne_fe_e', `turnout_pos_req_fe_de' , ///
				`turnout_pos_req_fe_e', `turnout_tot_req_fe_de' ,`turnout_tot_req_fe_e')	
	// clean .tex table for Overleaf 
		cleantex "$tables/Table_1_ES_ctr_ln_street_dist.tex" , nodisplay	replace

	
	// auxillary regression with Z-SCORE distance for comparision w/ Cantoni (2020)
	outreg, clear
	cap drop zscore_*
	zscore street_dist, stub(zscore_)
	reghdfe turnout_tot_req zscore_street_dist F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
	outreg, $opt keep(zscore_street_dist)	
	reghdfe turnout_urne zscore_street_dist F* L* 	$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
	outreg, $opt keep(zscore_street_dist)

	su street_dist [aw=wahlber_gesamt]
	di "One SD of Street Dist is equiv. to `:di %12.3f `r(sd)'' km and `:di %12.3f 0.621371 *`r(sd)'' miles"



********************************************************************************
*	Robustness to clustering: Baseline ES, treat=100%, smpl_trim, (Table C2)
*	Note for bootstrapping:
*		- reghdfe does not work with bootest + boottype(wild)
*		- areg does not work for TWOWAY clustering (does not throw error BUT OLS estimates CHANGE)
********************************************************************************

	* TABLE C2. Robustness to Clustering at Different Levels		
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen 	F`l'event = K==-`l'
		lab var F`l'event "Reassignment (#t-`l'#)"
	}	
	forvalues l = 0/7 {
		gen 	L`l'event = K==`l'
		lab var L`l'event "Reassignment (#t+`l'#)"
	}		
		drop 	F1event // drop reference period
		
		
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		outreg, clear
		
		*----- ELECTION-District FE	
		// (1) Baseline Event study (weighted) -- ELECTION-DISTRICT FE
		 reghdfe `v' F* L* 					$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new) 
		 outreg,  $opt  keep($order) addrow(Precinct FE, X \ Election-District FE, X \ Election-District FE,  \ Cluster Precinct, X \ Number of Clusters, 618)
	 
		// (2) TW cluster(District-Election + precinct) -- ELECTION-DISTRICT FE
		 reghdfe `v' F* L* 					$ctr 	if smpl_trim==1 	$wgt, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new i.stadtbez#i.wahl_id)
		 outreg,  $opt  keep($order) addrow(Precinct FE, X \ Election-District FE, X \ TW Cluster District-Election + Precinct, X \ Number of Clusters, 200+618)
		
		// (3) Wild Bootstrap cluster(Precinct) -- ELECTION-DISTRICT FE
		 areg `v' F* L* i.wahl_id#i.stadtbez $ctr 	if smpl_trim==1 	$wgt, absorb(sb_new) cluster(sb_new)
		bexp
		 outreg,  keep($order) stats(b se) brackets("","" \ [,]) nocons noleg nostars summstat(r2 \ N )  statfont(fs32 fnew1) sdec(2\3) varlabels merge ///
			addrow(Precinct FE, X \ Election-District FE, X \ WRC Precinct, X \ Number of Clusters, 618)	
		
		// (4) Wild Boostrap cluster(District) -- ELECTION-DISTRICT FE
		 areg `v' F* L* i.wahl_id#i.stadtbez $ctr 	if smpl_trim==1 	$wgt, absorb(sb_new) cluster(stadtbez)
		bexp
		 outreg,  keep($order) stats(b se) brackets("","" \ [,]) nocons noleg nostars summstat(r2 \ N )  statfont(fs32 fnew1) sdec(2\3) varlabels merge ///
			addrow(Precinct FE, X \ Election-District FE, X \ WRC District, X \ Number of Clusters, 25)	
				
		*----- ELECTION FE
		// (5) Wild Boostrap cluster(District) -- ONLY ELECTION FE
		 areg `v' F* L* i.wahl_id 	$ctr 	if smpl_trim==1 	$wgt, absorb(sb_new) cluster(stadtbez)
		bexp
		 outreg,  keep($order) stats(b se) brackets("","" \ [,]) nocons noleg nostars summstat(r2 \ N )  statfont(fs32 fnew1) sdec(2\3) varlabels merge ///
			addrow(Precinct FE, X \ Election FE, X  \ WRC District, X \ Number of Clusters, 25)	
			
		
		*save
		 outreg, replay replace store(`v')
	}
		 outreg, replay(turnout_urne) ctitle("", "\makecell{Cluster \\ Precinct \\ (baseline)}","\makecell{TW Cluster \\ Precinct+ \\ Election-District}", ///
				"\makecell{Wild Cluster \\ Bootstrap \\ Precinct}","\makecell{Wild Cluster \\ Bootstrap \\ District}","\makecell{Wild Cluster \\ Bootstrap \\ District}"\    ///
		"", (1), (2), (3), (4), (5) \ "\midrule" \ "\multicolumn{3}{l}{\textbf{Panel A:} Effect on Turnout at the Polling Place}" \"\midrule") store(tab1) ///
				addrow("\midrule" \ "\multicolumn{3}{l}{\textbf{Panel B:} Effect on Turnout via Mail}" \ "\midrule") 
		 outreg, replay(tab1) append(turnout_pos_req) store(tab_urne_postal_req)
		
		 outreg, replay(tab_urne_postal_req) store(tab3) ///
				addrow("\midrule" \ "\multicolumn{3}{l}{\textbf{Panel C:} Effect on Total Turnout}" \ "\midrule")  
		 outreg, replay(tab3) append(turnout_tot_req) store(tab_urne_postal_tot_req)
		
		
		// Export Table
		outreg using "$tables/Table_C2_ES_rob_clustering" , replay(tab_urne_postal_tot_req) replace tex fragment
		cleantex "$tables/Table_C2_ES_rob_clustering.tex" , nodisplay	replace


********************************************************************************
*	 Heterogeneity: Increase/Decrease Distance [Full Sample] (Table C3)
********************************************************************************
		
	* TABLE C3. Effect Heterogeneity by Change in Proximity to the Polling Place
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event
	}		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg,clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
	// (1) Baseline: Smpl_trim
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) ctitle("")

	// (2)  CLEAN sample (remove CTRL precincts with some NONZERO reass and TREAT precinct with 1+ treatment)
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b ///
				$ctr $wgt if cleanctr==1 & fulltottreat100<=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) addrow( Clean sample, X) ctitle("", "\multicolumn{3}{c}{`v'}")

				
	// (2) balanced SAMPLE
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b ///
				$ctr $wgt if smpl_bal==1 & fulltottreat100<=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) addrow(Balanced sample, X) ctitle("")			
			 		 	
	}

	
	// Export Regtable
	outreg using "$tables/Table_C3_ES_het_by_distance", replay tex replace fragment
	cleantex "$tables/Table_C3_ES_het_by_distance.tex" , nodisplay	replace
		

********************************************************************************
*	 Heterogeneity: Distance 3 groups (Table C4)
********************************************************************************		

	* TABLE C4. Effect Heterogeneity by Change in Proximity to the Polling Place (3 bins)
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	
	// Create two set of dummies: Reason Dummy x rel. time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_m1	// a := decrease
		gen	 	F`l'event_b = F`l'event *ind_dist_m2	// b:= middle
		gen 	F`l'event_c = F`l'event *ind_dist_m3	// c := increase
		assert  F`l'event_b+F`l'event_a+F`l'event_c==F`l'event		
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_m1	// a := decrease
		gen	 	L`l'event_b = L`l'event *ind_dist_m2	// b:= middle
		gen 	L`l'event_c = L`l'event *ind_dist_m3	// c := increases
		assert  L`l'event_b+L`l'event_a+L`l'event_c==L`l'event	
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"		
		
	}		
	
	// ORDER dummies
	order *event_b, last
	order *event_c, last
	order F1event*,last	

	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg, clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if smpl_bal==1 & fulltottreat100<=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)		
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) store(`v')
		 	

	}	
	
	// Estimate ES by 3 distance groups
	estimates clear
	outreg,clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
	// (1) Baseline: Smpl_trim
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) ctitle("")

	// (2)  CLEAN sample (remove CTRL precincts with some NONZERO reass and TREAT precinct with 1+ treatment)
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if cleanctr==1 & fulltottreat100<=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) addrow( Clean sample, X) ctitle("", "\multicolumn{3}{c}{`v'}")

				
	// (2) balanced SAMPLE
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if smpl_bal==1 & fulltottreat100<=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) addrow(Balanced sample, X) ctitle("")			
			 		 	
	}		
	outreg using "$tables/Table_C4_ES_het_by_distance_3clust", replay tex replace fragment
	cleantex "$tables/Table_C4_ES_het_by_distance_3clust.tex" , nodisplay	replace
